import { InputItem as AntInput } from 'antd-mobile-v2';
import * as React from 'react';
import { Props, State } from '../type';

export class Input extends React.Component<Props, State> {
  public static defaultProps = new Props();
  public state = new State();

  validator = (_rule: any, _val2: any, callback: any) => {
    const val = this.props.form.getFieldValue(this.props.code);
    const reg = /^[\s]+$/;
    if (reg.test(val)) {
      callback(new Error(`${this.props.name}不能为空`));
    }
    callback();
  };
  // 金额从科学计数法转为正确显示 9.99999999998e+23 -> 9999999999980000000000000
  toolNumber = (num) => {
    num = String(num);
    if (num.indexOf('+') != -1) {
      num = num.replace('+', '');
    }
    if (num.indexOf('E') != -1 || num.indexOf('e') != -1) {
      let resValue = '',
        power: string | number = '',
        result = null,
        dotIndex = 0,
        resArr = [],
        sym = '';
      let numStr = num.toString();
      if (numStr[0] == '-') {
        // 如果为负数，转成正数处理，先去掉‘-’号，并保存‘-’.
        numStr = numStr.substr(1);
        sym = '-';
      }
      if (numStr.indexOf('E') != -1 || numStr.indexOf('e') != -1) {
        const regExp = new RegExp(
          '^(((\\d+.?\\d+)|(\\d+))[Ee]{1}((-(\\d+))|(\\d+)))$',
          'ig'
        );
        result = regExp.exec(numStr);
        if (result != null) {
          resValue = result[2];
          power = result[5];
          result = null;
        }
        if (!resValue && !power) {
          return undefined;
        }
        dotIndex = resValue.indexOf('.') == -1 ? 0 : resValue.indexOf('.');
        resValue = resValue.replace('.', '');
        resArr = resValue.split('');
        if (Number(power) >= 0) {
          const subres = resValue.substr(dotIndex);
          power = Number(power);
          //幂数大于小数点后面的数字位数时，后面加0
          for (let i = 0; i <= power - subres.length; i++) {
            resArr.push('0');
          }
          if (power - subres.length < 0) {
            resArr.splice(dotIndex + power, 0, '.');
          }
        } else {
          power = Number(power);
          //幂数大于等于 小数点的index位置, 前面加0
          for (let i = 0; i < power - dotIndex; i++) {
            resArr.unshift('0');
          }
          const n = power - dotIndex >= 0 ? 1 : -(power - dotIndex);
          resArr.splice(n, 0, '.');
        }
      }
      resValue = resArr.join('');

      return sym + resValue;
    } else {
      return num;
    }
  };

  public render() {
    const {
      getFieldProps = () => {
        //
      }
    } = this.props.form || {};
    const {
      form,
      name,
      code,
      required,
      error,
      onErrorClick,
      ...otherProps
    } = this.props;
    let fieldValue = form ? form.getFieldValue(this.props.code) : '';
    if (name === '发放金额') {
      fieldValue = this.toolNumber(fieldValue);
    }
    return (
      <AntInput
        title={fieldValue}
        {...otherProps}
        {...getFieldProps(this.props.code, {
          rules: [
            { required: this.props.required, message: `请输入${name}` },
            { validator: this.validator }
          ]
        })}
        value={fieldValue}
      >
        {name}
      </AntInput>
    );
  }
}
